package com.soft.sitegenerator.action;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.NewsGroup;
import com.soft.sitegenerator.business.hibernate.User;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.MemberEditForm;

public class MemberEditAction extends DispatchAction {

	public ActionForward deleteMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		Long idUser = new Long(Long.parseLong(request.getParameter("idUser")));
		Long idGroup = new Long(Long.parseLong(request.getParameter("idGroup")));

		Component component = new DataBaseComponent();
		User user = (User) component.getBean("usersHib");
		user.setId(idUser);

		try {
			user.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		NewsGroup grupo = (NewsGroup) component.getBean("newsgroupsHib");
		grupo.setId(idGroup);

		try {
			grupo.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		NewsGroup group;
		for (Iterator it = user.getNewsGroups().iterator(); it.hasNext();) {
			group = (NewsGroup) it.next();
			if (grupo.getId().equals(group.getId()))
				it.remove();
		}

		user.update();

		return mapping.findForward("showList");
	}

	public ActionForward addMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		MemberEditForm memberEditForm = (MemberEditForm) form;

		Component component = new DataBaseComponent();
		NewsGroup newsGroup = (NewsGroup) component.getBean("newsgroupsHib");

		memberEditForm.setNewsGroups(newsGroup.findAll());

		User user = (User) component.getBean("usersHib");

		memberEditForm.setUsuarios(user.findAll());

		return mapping.findForward("showAdd");
	}

	public ActionForward saveMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MemberEditForm memberEditForm = (MemberEditForm) form;

		Component component = new DataBaseComponent();
		NewsGroup newsGroup = (NewsGroup) component.getBean("newsgroupsHib");

		newsGroup.setId(memberEditForm.getIdNewsGroup());

		try {
			newsGroup.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		User user = (User) component.getBean("usersHib");

		user.setId(memberEditForm.getIdUser());

		try {
			user.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		user.getNewsGroups().add(newsGroup);

		user.update();

		return mapping.findForward("showList");

	}

}